import numpy as np
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import LeaveOneOut

# 加载breast cancer数据集
X, y = datasets.load_breast_cancer(return_X_y=True)
X_b = X
y_b = y

# 加载wine数据集，并剔除第三类数据
X, y = datasets.load_wine(return_X_y=True)
X_w = X[0:130]
y_w = y[0:130]

# 创建对率回归模型
log_model = LogisticRegression(solver='liblinear')

# 对两数据集进行10折交叉验证法错误率估计
acc_b = cross_val_score(
    log_model, X_b, y_b, cv=10, scoring='accuracy'
)
acc_w = cross_val_score(
    log_model, X_w, y_w, cv=10, scoring='accuracy'
)
print(
    '\n10折交叉验证法在breast cancer数据集上的错误率：\n\t',
    (1 - acc_b).mean()
)
print(
    '\n10折交叉验证法在wine数据集上的错误率：\n\t',
    (1 - acc_w).mean()
)

# 对两数据集进行留一法错误率估计
loo = LeaveOneOut()

acc_b = 0
for train, test in loo.split(X_b):
    log_model.fit(X_b[train], y_b[train])
    y_p = log_model.predict(X_b[test])
    if y_p == y_b[test]:
        acc_b += 1
print(
    '\n留一法在breast cancer数据集上的错误率：\n\t',
    1 - float(acc_b) / np.shape(X_b)[0]
)

acc_w = 0
for train, test in loo.split(X_w):
    log_model.fit(X_w[train], y_w[train])
    y_p = log_model.predict(X_w[test])
    if y_p == y_w[test]:
        acc_w += 1
print(
    '\n留一法在wine数据集上的错误率：\n\t',
    1 - float(acc_w) / np.shape(X_w)[0]
)
